<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            width: 350px;
            height: 500px;
            background-color: pink;
            margin: 100px auto;
            box-sizing: border-box;
            padding: 50px 0;
        }

        .box h2 {
            text-align: center;
        }

        .box .title {
            font-weight: bold;
            line-height: 80px;
            text-align: center;
        }

        .box .block {
            margin: 30px 0;
            box-sizing: border-box;
            padding: 0 50px;
        }

        .box .block input {
            height: 40px;
            padding-left: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
            width: 100%;
            outline: none;
            box-sizing: border-box;
        }

        .box .block1 input {
            background-color: skyblue;
            color: white;
            font-weight: 700;
        }

        p {
            font-size: 12px;
            color: blue;
            text-align: center;
        }
    </style>
</head>

<body>
    <div class="box">
        <h2>阿里云盘</h2>
        <div class="title">注册新账号</div>
        <div class="block">
            <input type="text" placeholder="请输入账号">
        </div>
        <div class="block">
            <input type="password" placeholder="请输入密码">
        </div>
        <div class="block">
            <input type="password" placeholder="确认密码">
        </div>
        <div class="block block1">
            <input type="button" value="注册">
        </div>
        <p>阿里云盘净网专项行动</p>
    </div>
</body>

</html>
<script src="./promise-ajax.js"></script>
<script>
    //获取四个input输入框
    var inputs = document.querySelectorAll('.box .block input');
    //给button绑定点击事件
    inputs[3].onclick = function () {
        //对表单内容做校验( 为空校验, 格式校验 )
        //三个输入框都不能为空
        if (inputs[0].value && inputs[1].value && inputs[2].value) {
            if (/^1[3-9]\d{9}$/.test(inputs[0].value) && /^\d{4}$/.test(inputs[1].value) && /^\d{4}$/.test(inputs[2].value)) {
                //输入的密码和确认的密码一致
                if (inputs[1].value == inputs[2].value) {
                    ajax('get', 'https://yantianfeng.com/api/register', { username: inputs[0].value, password: inputs[1].value }).then((res) => {
                        if (res.status == 200) {
                            alert('注册成功');
                        } else {
                            alert('注册失败!');
                        }
                    })
                } else {
                    alert('密码不一致')
                }
            } else {
                alert('请输入正确格式的密码')
            }
        } else {
            alert('请输入账号/密码')
        }
    }
</script>